import util from '@/libs/util';
import Cookies from 'js-cookie';
import bookList from '../book/bookList.vue';

export default {
    name: 'exportable-table',
    components: {
        bookList
    },
    data () {
        return {
            columns: [{
                type: 'expand',
                width: 40,
                render: (h, params) => {
                    return h('Table', {
                        props: {
                            columns: this.columns,
                            data: this.childData['p_' + params.row.id],
                            'show-header': false
                        }
                    });
                }
            }, {
                title: '专业名称',
                key: 'name'
            }, {
                title: '专业代码',
                key: 'code'
            }, {
                title: '所属科目',
                key: 'subjectid',
                render: (h, params) => {
                    return h('div', {}, params.row.subject ? params.row.subject.name : '');
                }
            }, {
                title: '排序',
                key: 'orders'
            }, {
                title: '是否启用',
                key: 'status',
                render: (h, params) => {
                    return h('div', {}, params.row.status === 'YES' ? '启用' : '停用');
                }
            }, {
                title: '教材',
                key: 'booksname'
            }, {title: '操作',
                key: 'action',
                width: 500,
                align: 'center',
                render: (h, params) => {
                    let status = false;
                    if (params.row.status === 'YES') status = true;
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.detail(params.row.id);
                                }
                            }
                        }, '查看'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.showEdit(params.row.id);
                                }
                            }
                        }, '修改'),
                        h('Button', {
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.delete(params.row.id);
                                }
                            }
                        }, '删除'),
                        h('Button', {
                            props: {
                                type: 'warning',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.$Modal.confirm({
                                        content: '该操作会影响下级专业，是否继续' + (status ? '停用' : '启用') + '?',
                                        onOk: () => {
                                            this.setSatus(params.row.id, status ? 'NO' : 'YES');
                                        }
                                    });
                                }
                            }
                        }, (status ? '停用' : '启用')),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                display: params.row.level === 1 ? 'inline' : 'none'
                            },
                            on: {
                                click: () => {
                                    this.addChild(params.row);
                                }
                            }
                        }, '添加下级专业'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                display: params.row.level === 1 ? 'none' : 'inline'
                            },
                            on: {
                                click: () => {
                                    this.addTextBooks(params.row.id);
                                }
                            }
                        }, '添加教材')
                    ]);
                }
            }],
            baseuser: JSON.parse(Cookies.get('baseUser')),
            data: [],
            formData: {},
            query: {
                name: '',
                subjectid: ''
            },
            selectYesOrNo: [{id: 'YES', name: '是'}, {id: 'NO', name: '否'}],
            subjectList: [],

            total: 0,
            pageIndex: 1,
            pageSize: 10,

            editModal: false,
            childData: {},
            isRead: {
                parentid: false,
                subjectid: false
            },
            editTitle: '新增专业',
            bookModal: false,
            bookids: [],
            majorid: ''
        };
    },
    mounted () {
        this.loadDictionaries();
        this.loadList();
    },
    methods: {
        loadDictionaries () {
            util.ajax.post('/base/datadictionary/findByCode', util.toFormData({
                CODE: 'SUBJECT_TYPE'
            })).then(res => {
                this.subjectList = res.data.data;
            });
        },
        loadList () {
            util.ajax.post('/busine/major/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                name: this.query.name,
                subjectid: this.query.subjectid
                // organizeid: this.baseuser.orgId
            })).then(res => {
                this.data = res.data.data.list;
                this.total = res.data.data.total;
                this.data.forEach(item => {
                    item._disableExpand = !item.haveChild;
                });
            });
        },
        refresh () {
            this.query = {
                name: '',
                subjectid: ''
            };
            this.loadList();
        },
        showAdd () {
            this.editTitle = '新增专业'
            this.isRead = {};
            this.formData = {
                status: 'YES',
                issginup: 'NO',
                level: 1,
                organizeid: this.baseuser.orgId
            };
            this.editModal = true;
        },
        addChild (obj) {
            this.editTitle = '新增下级专业'
            this.isRead = {
                parentid: true,
                subjectid: true
            };
            this.formData = {
                parentid: obj.id,
                parentname: obj.name,
                subjectid: obj.subjectid,
                status: 'YES',
                issginup: 'NO',
                level: 2,
                organizeid: this.baseuser.orgId
            };
            this.editModal = true;
        },
        detail (id) {
            this.editTitle = '查看专业'
            util.ajax.post('/busine/major/detail', util.toFormData({
                id: id
            })).then(res => {
                this.formData = res.data.data;
                this.formData.parentname = this.formData.parent ? this.formData.parent.name : ''
                this.isRead = {
                    name: true,
                    code: true,
                    subjectid: true,
                    orders: true,
                    status: true,
                    issginup: true
                };
                this.editModal = true;
            });
        },
        showEdit (id) {
            this.editTitle = '修改专业'
            util.ajax.post('/busine/major/detail', util.toFormData({
                id: id
            })).then(res => {
                this.formData = res.data.data;
                if (this.formData.parentid) {
                    this.formData.parentname = this.formData.parent ? this.formData.parent.name : ''
                    this.isRead = {
                        status: true,
                        subjectid: true
                    };
                } else {
                    this.isRead = {
                        status: true,
                        subjectid: false
                    };
                }
                this.editModal = true;
            });
        },
        save () {
            let formData = {};
            for (var key in this.formData) {
                formData[key] = this.formData[key];
            }
            util.ajax.post('/busine/major/save', util.toFormData(formData)).then(res => {
                if (res.data.message === 'SUCCESS') {
                    this.$Message.success('保存成功！');
                    this.loadList();
                    this.editModal = false;
                }
            });
        },
        delete (id) {
            this.$Modal.confirm({
                content: '是否确认删除?',
                onOk: () => {
                    util.ajax.post('/busine/major/delete', util.toFormData({
                        id: id
                    })).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('删除成功！');
                            this.loadList();
                        }
                    });
                }
            });
        },
        setSatus (id, status) {
            util.ajax.post('/busine/major/setStatus', util.toFormData({
                id: id,
                status: status
            })).then(res => {
                if (res.data.message === 'SUCCESS') {
                    this.$Message.success('保存成功！');
                    this.loadList();
                }
            });
        },
        onExpand (row, status) {
            if (status) {
                util.ajax.post('/busine/major/findByParent', util.toFormData({
                    parentid: row.id
                })).then(res => {
                    let id = 'p_' + row.id;
                    res.data.data.forEach(item => {
                        item._disableExpand = !item.havechild;
                    });
                    this.$set(this.childData, id, res.data.data);
                });
            }
        },
        addTextBooks (id) {
            this.majorid = id;
            this.bookids = [];
            util.ajax.post('/busine/major/book/getByMajor', util.toFormData({
                majorid: id
            })).then(res => {
                res.data.data.forEach(item => {
                    this.bookids.push(item.bookid);
                });
                this.bookModal = true;
            });
        },
        saveBook () {
            let books = [];
            this.$refs.book.books.forEach(item => {
                books.push(item.id);
            });
            util.ajax.post('/busine/major/book/saveByMajor', util.toFormData({
                ids: books,
                majorid: this.majorid
            })).then(res => {
                if (res.data.message === 'SUCCESS') {
                    this.$Message.success('保存成功！');
                    this.loadList();
                    this.bookModal = false;
                }
            });
        }
    }
}